package gov.fnal.xpathsearch;

import gov.fnal.xpath2ldap.util.Configuration;

import java.net.URL;
import java.io.IOException;

import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.CommandLine;


public class XQueryToXPathOptions {

    public static final String XQUERYFILE = "q";
    public static final String XQUERYFILE_DEFAULT = "-";
    public static final String XQUERYSTRING = "s";
    public static final String XQUERYSTRING_DEFAULT = "";
    private static Options options;
    public static String version = "put_maven_version_filter_here";
    public static String syntax = "xquerytoxpath [options] [ <-q xqueryfile1 [ xqueryfile2 ] ...> ] [ <-s xquerystring1 [xquerystring2] ...> ]";

    public static String header = "Available options:";

    public static String footer = "\n" +
            "\nJava properties:\n" +
            "-Dxpathsearch.home=full path of xpathsearch installation directory\n" +
            "\nTry 'xquerytoxpath <command> --help' for help on a specific command.\n" +
            "For example 'xpathsearch --help'.\n" +
            "\n" +
            "XQueryToXPath -- Extracts XPath expressions from xquery files.\n" +
            "For additional information, see http://cd-97177.fnal.gov:2088/redmine\n" +
            "Bug Reports to <xpathsearch@gmail.com>";

    public static CommandLine processArguments(String[] args) {
        return Configuration.processArguments(args, getOptions(), version, syntax, header, footer);
    }

    public static Options getOptions() {
        if(options==null) setupOptions();
        return options;
    }

    private static void setupOptions() {
        // Read in extra properties from the conf subdirectory of the install directory.
        try {
            URL base_conf_url = Configuration.getModulePropertiesURL("xpathsearch.home");
            URL[] conf_urls = new URL[]{base_conf_url};
            Configuration.processModuleProperties("xpathsearch.properties", conf_urls);
        } catch (
                IOException ioe) {
            System.err.println("Warning: could not process properites file");
        }

        // create the Options
        options = new Options();
        options.addOption("v", "version", false, "");
        options.addOption("h", "help", false, "");
        Option xqueryfile   = OptionBuilder.hasArgs()
                .withArgName("XQUERYFILE")
                .withDescription(  "file containing xquery script. ")
                .create( XQUERYFILE );
        options.addOption(xqueryfile);
        Option xquerystring   = OptionBuilder.hasArgs()
                .withArgName("XQUERYSTRING")
                .withDescription(  "string containing xquery script. ")
                .create( XQUERYSTRING );
        options.addOption(xquerystring);
        options.addOption("t", "test", false, "Run the unit tests");
        options.addOption("i", "interactive", false, "Process the expressions interactively");
    }
}